//@author A0088797Y 
#include "UserInterface.h"

UserInterface::UserInterface(Logic* logPtr) {
	_logPtr = logPtr;
}

UserInterface::~UserInterface(void) {
}

void UserInterface::run(void) {
	std::string userInput;
	std::string output;
	bool isTerminate;

	do {
		try {
			userInput = read();
			isTerminate = _logPtr->executeCommand(userInput, output);
			printOutput(output);
		} catch(std::exception& exception) {
			std::cout << exception.what() << std::endl << std::endl;
			if(exception.what() == TASKERROR_INVALIDSTRINGFORMAT || exception.what() == TASKERROR_INVALIDTYPECODE || exception.what() == TASKERROR_INCORRECTEMPTYATTRIBUTESYMBOL || exception.what() == TASKERROR_NODESCRIPTION || exception.what() == TASKERROR_INVALIDDATETIME || exception.what() == TASKERROR_INVALIDDATE || exception.what() == TASKERROR_INVALIDTIME || exception.what() == TASKERROR_INCORRECTTYPECODE || exception.what() == TASKERROR_MDAYOUTOFRANGELEAPFEB || exception.what() == TASKERROR_MDAYOUTOFRANGENONLEAPFEB || exception.what() == TASKERROR_MDAYOUTOFRANGE30DAYMONTH || exception.what() == TASKERROR_MDAYOUTOFRANGE31DAYMONTH || exception.what() == TASKERROR_MONTHOUTOFRANGE || exception.what() == TASKERROR_HOUROUTOFRANGE || exception.what() == TASKERROR_MINOUTOFRANGE) {
				std::cout << "TEXT FILE IS CORRUPTED. PLEASE CHECK ActivityLog.txt FOR DETAILS." << std::endl;
				isTerminate = true;
			} else {
				isTerminate = false;
			}
		}
	} while(!isTerminate);

	return;
}

std::string UserInterface::read(void) {
	std::string userInput;

	getline(std::cin, userInput);

	if(userInput == "") {
		throw exceptionInputNoCommand;
	}

	return userInput;
}

void UserInterface::printOutput(std::string output) {
	std::cout << output << std::endl;
}